package com.pti.usermanager.service.impl;

import com.pti.usermanager.dao.api.OrgDao;
import com.pti.usermanager.dao.api.PostDao;
import com.pti.usermanager.dao.po.OrgPo;
import com.pti.usermanager.dao.po.PostPo;
import com.pti.usermanager.enums.ConstantCode;
import com.pti.usermanager.service.api.OrgService;
import com.pti.usermanager.service.api.PostService;
import com.pti.usermanager.web.dto.request.*;
import com.pti.usermanager.web.dto.response.ResultResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: PostServiceImpl
 * @Author: dongzhipeng
 * @Description: 岗位业务实现层
 * @Date: 2019/1/17
 */
@Service
public class PostServiceImpl implements PostService {


    private static final Logger logger = LogManager.getLogger(PostServiceImpl.class);

    @Autowired
    private PostDao postDao;

    @Autowired
    private OrgDao orgDao;

    /**
     * @Author dongzhipeng
     * @Description 树查询岗位
     * @Date 2019/1/17
     */
    @Override
    public ResultResponse findPostByDeptId(Long usrId,Long orgId) {
        logger.debug("usrId:{}",usrId);
        logger.debug("orgId:{}",orgId);

        Map<String, Object> inputMap = new HashMap<>();
        String roleCd = orgDao.findRoleByUsrId(usrId);
        inputMap.put("usrId",usrId);
        inputMap.put("roleCd",roleCd);
        inputMap.put("orgId",orgId);

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(postDao.findPostByDeptId(inputMap));
        return resultResponse;
    }

    @Override
    public ResultResponse findAllByPostId(Long postId) {
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(postDao.findAllByPostId(postId));
        return resultResponse;
    }

    @Override
    public ResultResponse savePost(SavePostRequest savePostRequest) {
        logger.debug("savePostRequest:{}",savePostRequest.toString());

        ResultResponse resultResponse = new ResultResponse();
        try {
            postDao.savePost(savePostRequest);
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }

    @Override
    public ResultResponse updatePost(UpdatePostRequest updatePostRequest) {

        logger.debug("updatePostRequest:{}",updatePostRequest.toString());

        ResultResponse resultResponse = new ResultResponse();

        try{
            postDao.updatePost(updatePostRequest);
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }

    @Override
    public ResultResponse delPost(Long compId) {
        logger.debug("compId:{}",compId);

        ResultResponse resultResponse = new ResultResponse();

        try{
            postDao.delPost(compId);
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }

    @Override
    public ResultResponse saveUserByPidAndUid(SaveUserByPidAndUid saveUserByPidAndUid) {
        logger.debug("saveUserByPidAndUid:{}",saveUserByPidAndUid.toString());

        ResultResponse resultResponse = new ResultResponse();

        try {
            postDao.deleteUserByPid(saveUserByPidAndUid.getPostId());
            for(Long usrId:saveUserByPidAndUid.getUsrId()){
                postDao.saveUserByPidAndUid(saveUserByPidAndUid.getPostId(),usrId);
            }
            return resultResponse;
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
            return resultResponse;
        }
    }
}
